package validators;

import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpSession;

import controllers.LocaleController;
import beans.UserBeanRemote;

@ManagedBean
public class UserNameValidator implements Validator {
/**
 * Ienei Robert
 * Validates the username when the admin creates a new one
 * 	-minimum 3 characters
 * 	-maximum 15
 * 	-accepted characters: a-z0-9_-
 */


	@EJB
	private UserBeanRemote ubr;
	
	public UserNameValidator() {
	}

	@Override
	public void validate(FacesContext arg0, UIComponent arg1, Object arg2)
			throws ValidatorException {

		String enteredName=(String) arg2;
		Pattern p = Pattern.compile("^[a-zA-Z0-9_-]{3,15}$");
		Matcher m = p.matcher(enteredName);
		boolean matchFound = m.matches();
		
		//internationalization
		HttpSession session = (HttpSession)
		FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		LocaleController language = (LocaleController) session.getAttribute("language");
		String messageBundleName = "errorMessages.errors";
		if (language.getLanguage()=="ro"){
			messageBundleName += "_ro";
		}
		else if (language.getLanguage()=="hu"){
			messageBundleName += "_hu";
		}
		else if (language.getLanguage()=="de"){
			messageBundleName += "_de";
		}
		ResourceBundle messageBundle = ResourceBundle
				.getBundle(messageBundleName);
		//////////
		
		if (enteredName.equals("")||(enteredName==null)){
			FacesMessage message = new FacesMessage();
			message.setDetail(messageBundle.getString("usernameEmpty"));
			message.setSummary(messageBundle.getString("usernameEmpty"));
			throw new ValidatorException(message);
		}
		else{
			if(enteredName.length()<3){
				FacesMessage message = new FacesMessage();
				message.setDetail(messageBundle.getString("usernameToShort"));
				message.setSummary(messageBundle.getString("usernameToShort"));
				throw new ValidatorException(message);
			}
			else{
				if (enteredName.length()>15){
					FacesMessage message = new FacesMessage();
					message.setDetail(messageBundle.getString("usernameTooLong"));
					message.setSummary(messageBundle.getString("usernameTooLong"));
					throw new ValidatorException(message);
				}

				else{
					if (!matchFound){
						FacesMessage message = new FacesMessage();
						message.setDetail(messageBundle.getString("usernameContainsUnapprovedCharacters"));
						message.setSummary(messageBundle.getString("usernameContainsUnapprovedCharacters"));
						throw new ValidatorException(message);
					}
					else{
						if ((ubr.existsNameInDB(enteredName)) ){
							FacesMessage message = new FacesMessage();
							message.setDetail(messageBundle.getString("usernameAlreadyExists"));
							message.setSummary(messageBundle.getString("usernameAlreadyExists"));
							throw new ValidatorException(message);
						}
					}
				}
			}
		}
	}
}
